package com.twang.stock.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.jayway.jsonpath.JsonPath;
import com.sun.javafx.binding.StringFormatter;
import com.twang.stock.common.http.HttpClientService;
import com.twang.stock.dto.MatchDetails;
import com.twang.stock.dto.MatchWanZhuBeiDTO;
import com.twang.stock.dto.WanZhuHotUserDto;
import com.twang.stock.entity.Holiday;
import com.twang.stock.entity.WanZhuHotStock;
import com.twang.stock.mapper.WanZhuHotStockMapper;
import com.twang.stock.redis.RedisUtil;
import com.twang.stock.service.jinyan.CommonService;
import com.twang.stock.utils.date.HolidayCopy;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * 玩主-持仓
 */
@Slf4j
@Service
public class WanZhuBeiChiChangService {
    private static String RUL = "https://api.hunanwanzhu.com/api/wx/v2/hot/stock/buy/list";

    private static String HotUSER = "https://api.hunanwanzhu.com/api/wx/v2/hot/follow/list";

    private static String 千万组="https://api.hunanwanzhu.com/api/wx/group/rank?id=136&order_by=total_income__desc&page=1&page_size=50&stock_date=2024-12-06";
    private static String 比赛列表="https://api.hunanwanzhu.com/api/wx/v2/match/list";

    private static String 比赛列表Group="https://api.hunanwanzhu.com/api/wx/group/rank/list";


    public static String WAN_ZHU_COOKIE_KEY="wan_zhu_cookie";


    public static String WAN_ZHU_HOT_DATA_KEY="wanzhu:hotuser:%s";

    @Autowired
    private RedisUtil redisUtil;


    @Autowired
    private CommonService commonService;

    @Resource
    private WanZhuHotStockMapper wanZhuHotStockMapper;

    public List<JSONObject>   获取比赛列表() {
        List<NameValuePair> nameValuePairList = Lists.newArrayList();
        Object result = HttpClientService.sendGet(比赛列表, nameValuePairList, "");
        String data = JSONObject.toJSONString(result);
        List<JSONObject> obj = JsonPath.read(data, "$.data");
        return obj;
    }

    public List<MatchWanZhuBeiDTO>   获取比赛列表Group(String matchId,String stockDate) {
        List<NameValuePair> nameValuePairList = Lists.newArrayList();
        nameValuePairList.add(new BasicNameValuePair("match_id", matchId));
        nameValuePairList.add(new BasicNameValuePair("stock_date", stockDate));
        Object result = HttpClientService.sendGet(比赛列表Group, nameValuePairList, "");
        String data = JSONObject.toJSONString(result);
        Object obj = JsonPath.read(data, "$.data");
        return JSONObject.parseArray(JSONObject.toJSONString(obj), MatchWanZhuBeiDTO.class);
    }


    public List<MatchDetails>  千万组(String id){
        List<NameValuePair> nameValuePairList = Lists.newArrayList();
        nameValuePairList.add(new BasicNameValuePair("id", id));//136
        nameValuePairList.add(new BasicNameValuePair("order_by", "total_income__desc"));
        nameValuePairList.add(new BasicNameValuePair("page", "1"));
        nameValuePairList.add(new BasicNameValuePair("page_size", "20"));
        nameValuePairList.add(new BasicNameValuePair("stock_date", "2024-12-06"));
        //调用服务
//        Object result = HttpClientService.sendGet(RUL, nameValuePairList, commonService.getValue(WAN_ZHU_COOKIE_KEY));
        Object result = HttpClientService.sendGet(千万组, nameValuePairList, "");

        String data = JSONObject.toJSONString(result);
        Object obj = JsonPath.read(data, "$.data.players");
        List<MatchDetails> list = JSONObject.parseArray(JSONObject.toJSONString(obj), MatchDetails.class);
        list.stream().forEach(item->{
            ObjectMapper objectMapper = new ObjectMapper();
            // 解析 JSON 字符串为 List<String>
            try {
                //today
                List<String> urls = objectMapper.readValue(item.getTodayStockImg(), List.class);
                item.setTodayStockImg(urls.get(0));
                //yesterday
                urls = objectMapper.readValue(item.getYesterdayStockImg(), List.class);
                item.setYesterdayStockImg(urls.get(0));

            } catch (IOException e) {
                log.error(">>>>>>>>解析 JSON 字符串为 eroor ",e);
            }

        });
        return list;
    }


    public  void 根据日期更新玩主杯热榜(String date){
        List<NameValuePair> nameValuePairList = Lists.newArrayList();
        nameValuePairList.add(new BasicNameValuePair("fund_rank", "0"));
        nameValuePairList.add(new BasicNameValuePair("page", "1"));
        nameValuePairList.add(new BasicNameValuePair("page_size", "20"));
        nameValuePairList.add(new BasicNameValuePair("stock_date", date));
        //调用服务
//        Object result = HttpClientService.sendGet(RUL, nameValuePairList, commonService.getValue(WAN_ZHU_COOKIE_KEY));
        Object result = HttpClientService.sendGet(RUL, nameValuePairList, "");

        log.info(">>>>>>>>>>> result={}", JSON.toJSONString(result));
        String data = JSONObject.toJSONString(result);
        Object obj = JsonPath.read(data, "$.data.list");
        List<WanZhuHotStock> list = JSONObject.parseArray(JSONObject.toJSONString(obj), WanZhuHotStock.class);
        if (CollectionUtils.isNotEmpty(list)) {
            for (int i = 0; i < list.size(); i++) {
                WanZhuHotStock wanZhuHotStock = list.get(i);
                wanZhuHotStock.setRowNo(i + 1);
                wanZhuHotStock.setDate(date);
                //
                WanZhuHotStock param = new WanZhuHotStock();
                param.setDate(date);
                param.setStockName(wanZhuHotStock.getStockName());
                wanZhuHotStockMapper.delete(param);
                wanZhuHotStockMapper.insert(wanZhuHotStock);
            }
        }
    }

    public  List<WanZhuHotUserDto>  查看热门选手(String date) {
        List<WanZhuHotUserDto> list;
        if (StringUtils.isEmpty(date)) {
            date = HolidayCopy.getRealToday();
        }
        String hotKey = String.format(WAN_ZHU_HOT_DATA_KEY, date);
        if (redisUtil.hasKey(hotKey)) {
            return JSONObject.parseArray(redisUtil.get(hotKey).toString(), WanZhuHotUserDto.class);
        }
        List<NameValuePair> nameValuePairList = Lists.newArrayList();
        nameValuePairList.add(new BasicNameValuePair("page", "1"));
        nameValuePairList.add(new BasicNameValuePair("page_size", "20"));
        nameValuePairList.add(new BasicNameValuePair("stock_date", date));
        //调用服务
        Object result = HttpClientService.sendGet(HotUSER, nameValuePairList, "");
        Object obj = JsonPath.read(result, "$.data.list");
        list = JSONObject.parseArray(JSONObject.toJSONString(obj), WanZhuHotUserDto.class);
        if (CollectionUtils.isNotEmpty(list)) {
            redisUtil.set(hotKey, JSONObject.toJSONString(list));
        }
        return list;
    }




    /**
     * 查询当日热榜
     * @param date
     */
    public   List<WanZhuHotStock>  查询当日热榜(String date) {
        WanZhuHotStock param = new WanZhuHotStock();
        param.setDate(date);
        List<WanZhuHotStock> wanZhuHotStocks = wanZhuHotStockMapper.select(param);
        if (CollectionUtils.isEmpty(wanZhuHotStocks)) {
            param.setDate(wanZhuHotStockMapper.getLastDate());
            wanZhuHotStocks = wanZhuHotStockMapper.select(param);
        }
        return wanZhuHotStocks;
    }

}
